package com.ruoyi.specific.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.ruoyi.common.excel.ExcelListener;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.ValidatorUtils;
import com.ruoyi.common.utils.spring.SpringUtils;

import com.ruoyi.specific.domain.vo.ImprovingInstanceVo;
import com.ruoyi.specific.domain.vo.ImprovingInstanceImportVo;
import com.ruoyi.specific.domain.bo.ImprovingInstanceBo;
import com.ruoyi.specific.service.IImprovingInstanceService;

import java.util.List;
import static com.ruoyi.common.core.mapper.BaseMapperPlus.log;

public class ImprovingInstanceImportListener extends AnalysisEventListener<ImprovingInstanceImportVo> implements ExcelListener<ImprovingInstanceImportVo> {
    private final IImprovingInstanceService iImprovingInstanceService;
    private final Boolean isUpdateSupport;
    private final String operName;
    private int successNum = 0;
    private int failureNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public ImprovingInstanceImportListener(Boolean isUpdateSupport) {
        this.iImprovingInstanceService = SpringUtils.getBean(IImprovingInstanceService.class);
        this.isUpdateSupport = isUpdateSupport;
        this.operName = LoginHelper.getUsername();
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    @Override
    public ExcelResult<ImprovingInstanceImportVo> getExcelResult() {
        return new ExcelResult<ImprovingInstanceImportVo>() {

            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<ImprovingInstanceImportVo> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }

        };
    }

    @Override
    public void invoke(ImprovingInstanceImportVo importVo, AnalysisContext analysisContext) {
        ImprovingInstanceVo vo = this.iImprovingInstanceService.queryById(importVo.getId());
        try {
            // 验证是否存在这个信息单号
            if (ObjectUtil.isNull(vo)) {
                ImprovingInstanceBo bo = BeanUtil.toBean(importVo, ImprovingInstanceBo.class);
                ValidatorUtils.validate(bo);
                bo.setCreateBy(operName);
                iImprovingInstanceService.insertByBo(bo);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、积分项目 ").append(importVo.getId()).append(" 导入成功");
            } else if (isUpdateSupport) {
                Long id = vo.getId();
                ImprovingInstanceBo bo = BeanUtil.toBean(importVo, ImprovingInstanceBo.class);
                bo.setId(id);
                ValidatorUtils.validate(bo);
                bo.setUpdateBy(operName);
                iImprovingInstanceService.updateByBo(bo);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、项目 ").append(importVo.getId()).append(" 更新成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>").append(failureNum).append("、项目 ").append(importVo.getId()).append(" 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、持续改善实例 " + importVo.getId() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }
}
